module(..., package.seeall)

--[[

**********************************************
* Sprite Sheet Packer for Corona ImageSheets *
**********************************************

Version 1.0 - 3/13/2012
Inital Release for Corona SDK 
Requires Build 2012.759 or later
Download Sprite Sheet Packer from here:
http://spritesheetpacker.codeplex.com/

***********
* Credits *
***********

Sprite Sheet Packer: Nick Gravelyn
Corona Implementation: Michael Wilson

***********
* Support *
***********

If this saved you some time, think about buying our games...

Michael's Game:
http://tilt-a-bowl.com/
Nick's Game:
http://nodehackgame.com/


-]]

function loadSheet ( ssFilename )
    local ssPath = system.pathForFile( ssFilename, system.ResourceDirectory )
    local ssFile = io.open( ssPath, "r" )
    
    -- Open Sprite Sheet Packer .TXT file 
    if ssFile then -- nil if no file found
        ssContents = ssFile:read( "*a" )
        print( "Contents of " .. ssPath .. "\n" .. ssContents )
        io.close( ssFile )
    else
        print( "File not found")
        return 0
    end
    
    -- Explode data to Temp Table
    local ssTemp = {}
    local i = 1
    for w in string.gmatch(ssContents, "[%w_-]+") do
        ssTemp[i] = w
        i = i + 1
    end
    
    -- Pack data into new ImageSheet format
    local ssOptions = { frames ={} }
    local j = 1
    for i=1,#ssTemp,5  do
        print("local " .. ssTemp[i] .. " = " .. j )
        ssOptions.frames[j] = { 
        x = tonumber(ssTemp[i+1]), y = tonumber(ssTemp[i+2]),
        width = tonumber(ssTemp[i+3]), height = tonumber(ssTemp[i+4]) }
        j = j + 1
    end
    ssTemp = nil
    
    -- That's all folks
    return ssOptions
end